
// 读取excel 转成数组
const xlsx = require('node-xlsx')
const fs = require('fs')
const { log } = require('console')
const outPath = __dirname + '/echartsData.js' //输出文件
const url = `${__dirname}/1.xlsx`
let outStr = '' // 最终输出的文字

const workbook = xlsx.parse(url)

let arr = workbook[0].data


let outArr = []





// status 参数值
const statusArr = [
    {
        name: '引入期',
        value: 1
    },
    {
        name: '加速期',
        value: 2
    },
    {
        name: '洗牌期',
        value: 3
    },
    {
        name: '出清期',
        value: 4
    },
    {
        name: '稳态期',
        value: 5
    },
    {
        name: '成熟后期',
        value: 6
    },
    {
        name: '衰退期',
        value: 7
    },
]
outStr += '\r\nexport const statusMenu=' + JSON.stringify(statusArr)
const typeArr = [
    {
        name: '风电',
        value: 1
    },
    {
        name: '储能',
        value: 2
    },
    {
        name: '氢能',
        value: 3
    },
    {
        name: '光伏',
        value: 4
    },
    {
        name: '高端装备制造',
        alias: '制造',
        value: 5
    },
    {
        name: '新材料',
        value: 6
    },
    {
        name: '其他',
        value: 7
    }
]
outStr += '\r\nexport const typeMenu=' + JSON.stringify(typeArr)
const levelArr = [
    {
        name: '全链',
        value: 1
    },
    {
        name: '上下游',
        value: 2
    },
    {
        name: '一级',
        value: 3
    },
    {
        name: '二级',
        value: 4
    },
    {
        name: '三级',
        value: 5
    },
]
outStr += '\r\nexport const levelMenu=' + JSON.stringify(levelArr)

const hotArr = [
    {
        name: '高',
        value: 1
    },
    {
        name: '较高',
        value: 2
    },
    {
        name: '中等',
        value: 3
    },
    {
        name: '较低',
        value: 4
    },
    {
        name: '低',
        value: 5
    },
]
outStr += '\r\nexport const hotMenu=' + JSON.stringify(hotArr)



// 根据名称生成 产业 type
const getType = (name) => {
    let type = 7
    typeArr.some(v => {
        const uname = v.alias ? v.alias : v.name
        if (name.includes(uname)) {
            type = v.value
            return true
        }
        return false
    })
    return type
}




// 对应excel 的列
const keyName = ['id', '所属国民经济行业分类', '产业年龄', '阶段new', '营业收入(同比增长率)', '净利润(同比增长率)', '销售毛利率', '净资产收益率ROE', '存货周转率', '资产负债率', '现金净流量增长率', '总分']

const keyArr = ['id', 'name', 'toYear', 'status', 'operatingRevenue', 'netProfit', 'grossProfit', 'netAssets', 'inventory', 'debt', 'cashFlow', 'total']
const name = []
const addName = (str) => {
    const index = name.findIndex(v => v == str)
    if (index === -1) {
        name.push(str)
    }
}
const getKey = (str, arr) => {
    const index = arr.findIndex(v => v.name == str)
    return index > -1 ? statusArr[index].value : 0
}

arr.forEach(item => {
    let obj = {}
    Array(12).fill(0).forEach((v, index) => {

        if (index === 3) {
            const key = getKey(item[index], statusArr)
            obj[keyArr[index]] = key
            obj.statusName = item[index]
        } else if (index === 2) {
            // 值顺便生成下上一年的值
            const size = item[index]
            obj[keyArr[index]] = item[index]
            let randomNumber = Math.floor(Math.random() * 21) - 10;
            const prevSize = size + randomNumber
            obj.prevYear = prevSize > 100 ? 100 : prevSize < 0 ? 0 : prevSize


        }
        else if (index === 1) {
            // 如果是name 名称这里，根据名称生成  type  level
            obj[keyArr[index]] = item[index]
            obj.type = getType(item[index])
            // 产业层级无法判断随机
            obj.level = Math.floor(Math.random() * levelArr.length - 1) + 1
            obj.hot = Math.floor(Math.random() * hotArr.length - 1) + 1

        }
        else {
            obj[keyArr[index]] = item[index]
        }
    })
    outArr.push(obj)
});
console.log(name);

const objs = {};

outArr = outArr.filter((v, index) => {
    if(!v.status) return false
    if (!objs[v.status]) {
        objs[v.status] = 0;
    }
    objs[v.status]++;
    if (objs[v.status] <10) {
        return true;
    }
    return false;
});


outStr += `\r\n export const echartsData=${JSON.stringify(outArr)}`
fs.writeFile(outPath, outStr, { flag: 'w' }, (err) => {
    if (!err) {
        console.log('成功')
    } else {
        console.log(err)
    }

});

